<?php


namespace App\Models\Admin;


use App\Log\AdminGroupClientLog;
use Illuminate\Support\Facades\DB;

class AdminGroupClient extends Base
{
    const OPERATE_PID = 1;
    const MODEL = 'admin_group_client';

    protected $table = 'admin_group_client';
    protected $guarded = [];
    protected $dateFormat = 'U';

    public function company()
    {
        return $this->hasMany(Company::class,'group_client_id','id');
    }

    public function adminRegion()
    {
        return $this->belongsTo(AdminRegion::class ,'base_address','id');
    }

    public function companyDomain()
    {
        return $this->belongsTo(Company::class,'company_id_domain','id');
    }

    public static function getList($where=[],$skip=0,$size=10)
    {
        $list=self::getwhere($where)
            ->with([
                'company:id,group_client_id,company_name,domain_name',
            ])
            ->skip($skip)->take($size)
            ->get()->transform(function ($i)
            {
                return $i;
            });
        return $list;
    }

    public static function getCount($where)
    {
        $count=self::getWhere($where)->count();
        return $count;
    }

    public static function getWhere($where)
    {
        $select=self::where([]);
        if(!empty($where['keyword']))
        {
            $keyword='%'.$where['keyword'].'%';
            $select=$select->where(function ($query) use ($keyword)
            {
                $query->where('name','like',$keyword)->orWhere('head','like',$keyword)->orWhere('mobile','like',$keyword);
            });
        }
        if(!empty($where['county_id']))
        {
            $select=$select->where('base_address',$where['county_id']);
        }
        else if(!empty($where['city_id']))
        {
            $ids_arr=AdminRegion::getCountyIdsByPid($where['city_id'],2);
            if(!empty($ids_arr))
            {
                $ids_arr=implode(',',$ids_arr);
                $select=$select->where(DB::raw('base_address in ('.$ids_arr.')'),1);
            }
        }
        else if(!empty($where['province_id']))
        {
            $ids_arr=AdminRegion::getCountyIdsByPid($where['province_id']);
            if(!empty($ids_arr))
            {
                $ids_arr=implode(',',$ids_arr);
                $select=$select->where(DB::raw('base_address in ('.$ids_arr.')'),1);
            }
        }
        return $select;
    }

    public static function getDetail($id)
    {
        $info=self::with([
            'adminRegion:id,mername',
            'company:id,group_client_id,company_name'
        ])->where('id',$id)->first();
        $info->mername=$info->adminRegion->mername;
        unset($info->adminRegion);
        return $info;
    }

    public static function addLog($pre,$next,$type,$method='')
    {
        $info = AdminOperateModule::where('pid',self::OPERATE_PID)->where('type',$type)->where('model',self::MODEL)->when($method,function ($query)use ($method){
            $query->where('method',$method);
        })->first();
        if (!empty($info)){
            $adminLog = new AdminGroupClientLog($pre,$next,$info);
            $adminLog->addLog();
        }
    }
}
